package com.example.healthmanagement.controller;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.healthmanagement.dao.ChildInformationHisRepository;
import com.example.healthmanagement.entity.ChildInformation;
import com.example.healthmanagement.entity.ChildInformationHis;
import com.example.healthmanagement.entity.vo.FoodRemVo;
import com.example.healthmanagement.mapper.ChildDietaryRecordsMapper;
import com.example.healthmanagement.mapper.ChildinformationtableMapper;
import com.example.healthmanagement.mapper.FoodinfoMapper;
import com.example.healthmanagement.pojo.ChildDietaryRecords;
import com.example.healthmanagement.pojo.Childinformationtable;
import com.example.healthmanagement.pojo.DTO.FoodDto;
import com.example.healthmanagement.pojo.Foodinfo;
import com.example.healthmanagement.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chen
 * @since 2024-03-08
 */
@RestController
@RequestMapping("/childinformationtable")
public class ChildinformationtableController {
    @Autowired
    ChildinformationtableService childInfoService;
    @Autowired
    ChildInformationHisService childInformationHisService;
    ChildInformationHisRepository childInformationHisRepository;

    @Autowired
    FoodRemService foodRemService;


    @Autowired
    UserService userService;


    @Autowired
    ChildinformationtableMapper childinformationtableMapper;

    @Autowired
    ChildDietaryRecordsMapper childDietaryRecordsMapper;

    @Autowired
    FoodinfoMapper foodinfoMapper;

    @GetMapping("")
    public List<Childinformationtable> getSportRecords(Childinformationtable foodinfo) {
        List<Childinformationtable> objectList = childInfoService.list(new LambdaQueryWrapper<Childinformationtable>().eq(foodinfo.getBirthdate() != null, Childinformationtable::getBirthdate, foodinfo.getBirthdate())
                .eq(foodinfo.getChildName() != null, Childinformationtable::getChildName, foodinfo.getChildName())
                .eq(foodinfo.getHeight() != null, Childinformationtable::getHeight, foodinfo.getHeight())
        );
        return objectList;
    }
    @PostMapping("")
    public boolean saveSportRecords(@RequestBody Childinformationtable foodinfo) {

        return childInfoService.save(foodinfo);
    }
    @PutMapping ("")
    public boolean updateSportRecords(@RequestBody Childinformationtable foodinfo) {
        //获取原来数据
        ChildInformation child =  userService.getChildByID(foodinfo.getChildID());
        double tempWeight = child.getWeight();
        double tempHeight = child.getHeight();
        String tempBmi = child.getBmi();
        double tempValue = Double.parseDouble(tempBmi);
        int age = child.getAge();
        int childId = child.getChildID();

        ChildInformationHis childInformationHis = new ChildInformationHis();
        childInformationHis.setWeight(tempWeight);
        childInformationHis.setHeight(tempHeight);
        childInformationHis.setAge(age);
        childInformationHis.setBmi(tempValue);
        LocalDateTime time= LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = time.format(formatter);

        childInformationHis.setCreateTime(formattedTime);
        childInformationHis.setChildId(childId);

        userService.addChildInFormationHis(childInformationHis);



        double weight = foodinfo.getWeight();
        double height = foodinfo.getHeight();
        DecimalFormat decimalFormat = new DecimalFormat("#.#");

        // 使用 DecimalFormat 格式化 float 值
        String formattedValue = decimalFormat.format(weight/(height/100*height/100));
        double value = Double.parseDouble(formattedValue);
        foodinfo.setBmi(value);


        return childInfoService.updateById(foodinfo);
    }

    @GetMapping("/his")
    public List<ChildInformationHis> getAllChildInformationHis(){
        return userService.getChildHisList();
    }
    @DeleteMapping  ("/{id}")
    public boolean DeleteSportRecords(@PathVariable("id") Long[] id) {

        return childInfoService.removeByIds(Arrays.asList(id));
    }

    /*获取食物推荐*/
    // 传用户id
    @GetMapping("/getFoodSuggest")
    public Object getFoodSuggest(@RequestParam int id) {
        Childinformationtable childinformationtable = childinformationtableMapper.selectById(id);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("DATE(createtime) = {0}", LocalDate.now());
        List<ChildDietaryRecords> childDietaryRecordsList = childDietaryRecordsMapper.selectList(queryWrapper);

        if (childDietaryRecordsList.isEmpty()) {
            return new ArrayList();
        }


        // '谷类','薯类','豆类','蔬菜类','动物类','纯热能类'
        HashMap<String, Integer> map = new HashMap<String, Integer>() {
            {
                put("谷类", 0);
                put("薯类", 0);
                put("豆类", 0);
                put("蔬菜类", 0);
                put("动物类", 0);
                put("纯热能类", 0);
            }
        };
        // 便利foodList
        for (ChildDietaryRecords childDietaryRecords : childDietaryRecordsList) {
            FoodDto[] foodsList = JSON.parseObject(childDietaryRecords.getFood(), FoodDto[].class);

            // 遍历foodList
            for (FoodDto food : foodsList) {
                Foodinfo foodinfo = foodinfoMapper.selectById(food.getId());
                food.category = foodinfo.getCategory();
                map.put(food.category, map.get(food.category) + food.number * food.energy);
            }
        }

        if (2 <= childinformationtable.getAge() && childinformationtable.getAge() <= 3) {
            List<String> categoryArr = new ArrayList<>();
            if (!(map.get("谷类") >= 75 && map.get("谷类") <= 125)) {
                categoryArr.add("谷类");
            }

            if (!(map.get("薯类") >= 75 && map.get("薯类") <= 125)) {
                categoryArr.add("薯类");
            }

            if (!(map.get("豆类") >= 5 && map.get("豆类") <= 15)) {
                categoryArr.add("豆类");
            }

            if (!(map.get("蔬菜类") >= 200 && map.get("蔬菜类") <= 400)) {
                categoryArr.add("蔬菜类");
            }

            if (!(map.get("动物类") >= 500 && map.get("动物类") <= 700)) {
                categoryArr.add("动物类");
            }

            if (!(map.get("纯热能类") >= 10 && map.get("纯热能类") <= 20)) {
                categoryArr.add("纯热能类");
            }


            QueryWrapper qw = new QueryWrapper<>();

            qw.in("category", categoryArr);

            return foodinfoMapper.selectList(qw);
        } else if (4 <= childinformationtable.getAge() && childinformationtable.getAge() <= 5) {
            List<String> categoryArr = new ArrayList<>();
            if (!(map.get("谷类") >= 100 && map.get("谷类") <= 150)) {
                categoryArr.add("谷类");
            }

            if (!(map.get("薯类") >= 100 && map.get("薯类") <= 150)) {
                categoryArr.add("薯类");
            }

            if (!(map.get("豆类") >= 15 && map.get("豆类") <= 20)) {
                categoryArr.add("豆类");
            }

            if (!(map.get("蔬菜类") >= 300 && map.get("蔬菜类") <= 550)) {
                categoryArr.add("蔬菜类");
            }

            if (!(map.get("动物类") >= 500 && map.get("动物类") <= 700)) {
                categoryArr.add("动物类");
            }

            if (!(map.get("纯热能类") >= 20 && map.get("纯热能类") <= 25)) {
                categoryArr.add("纯热能类");
            }


            QueryWrapper qw = new QueryWrapper<>();

            qw.in("category", categoryArr);

            return foodinfoMapper.selectList(qw);
        } else if (6 <= childinformationtable.getAge() && childinformationtable.getAge() <= 10) {
            List<String> categoryArr = new ArrayList<>();
            if (!(map.get("谷类") >= 175 && map.get("谷类") <= 250)) {
                categoryArr.add("谷类");
            }

            if (!(map.get("薯类") >= 175 && map.get("薯类") <= 250)) {
                categoryArr.add("薯类");
            }

            if (!(map.get("豆类") >= 15 && map.get("豆类") <= 15)) {
                categoryArr.add("豆类");
            }

            if (!(map.get("蔬菜类") >= 450 && map.get("蔬菜类") <= 500)) {
                categoryArr.add("蔬菜类");
            }

            if (!(map.get("动物类") >= 405 && map.get("动物类") <= 420)) {
                categoryArr.add("动物类");
            }

            if (!(map.get("纯热能类") >= 20 && map.get("纯热能类") <= 25)) {
                categoryArr.add("纯热能类");
            }


            QueryWrapper qw = new QueryWrapper<>();

            qw.in("category", categoryArr);

            return foodinfoMapper.selectList(qw);
        } else if (11 <= childinformationtable.getAge() && childinformationtable.getAge() <= 13) {
            List<String> categoryArr = new ArrayList<>();
            if (!(map.get("谷类") >= 250 && map.get("谷类") <= 300)) {
                categoryArr.add("谷类");
            }

            if (!(map.get("薯类") >= 250 && map.get("薯类") <= 300)) {
                categoryArr.add("薯类");
            }

            if (!(map.get("豆类") >= 15 && map.get("豆类") <= 15)) {
                categoryArr.add("豆类");
            }

            if (!(map.get("蔬菜类") >= 600 && map.get("蔬菜类") <= 750)) {
                categoryArr.add("蔬菜类");
            }

            if (!(map.get("动物类") >= 440 && map.get("动物类") <= 450)) {
                categoryArr.add("动物类");
            }

            if (!(map.get("纯热能类") >= 25 && map.get("纯热能类") <= 30)) {
                categoryArr.add("纯热能类");
            }


            QueryWrapper qw = new QueryWrapper<>();

            qw.in("category", categoryArr);

            return foodinfoMapper.selectList(qw);
        } else if (14 <= childinformationtable.getAge() && childinformationtable.getAge() <= 17) {
            List<String> categoryArr = new ArrayList<>();
            if (!(map.get("谷类") >= 300 && map.get("谷类") <= 400)) {
                categoryArr.add("谷类");
            }

            if (!(map.get("薯类") >= 300 && map.get("薯类") <= 400)) {
                categoryArr.add("薯类");
            }

            if (!(map.get("豆类") >= 15 && map.get("豆类") <= 25)) {
                categoryArr.add("豆类");
            }

            if (!(map.get("蔬菜类") >= 750 && map.get("蔬菜类") <= 850)) {
                categoryArr.add("蔬菜类");
            }

            if (!(map.get("动物类") >= 450 && map.get("动物类") <= 500)) {
                categoryArr.add("动物类");
            }

            if (!(map.get("纯热能类") >= 25 && map.get("纯热能类") <= 30)) {
                categoryArr.add("纯热能类");
            }


            QueryWrapper qw = new QueryWrapper<>();

            qw.in("category", categoryArr);

            return foodinfoMapper.selectList(qw);
        }else{
            return new ArrayList();
        }
    }

    /*获取饮食推荐推荐*/
    // 传用户id
    @GetMapping("/getFoodSuggest2")
    public Object getFoodSuggest2(@RequestParam int id) {
        // 饮食推荐
        StringBuilder suggest = new StringBuilder();
        Childinformationtable childinformationtable = childinformationtableMapper.selectById(id);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("DATE(createtime) = {0}", LocalDate.now());
        List<ChildDietaryRecords> childDietaryRecordsList = childDietaryRecordsMapper.selectList(queryWrapper);

        if (childDietaryRecordsList.isEmpty()) {
            suggest = new StringBuilder("你的营养十分均衡，所以对您没有任何建议");
            Map<String,Object> mm = new HashMap<>();
            mm.put("suggest", suggest.toString());
            mm.put("foodList",new ArrayList<>());
            return mm;
        }

        // '谷类','薯类','豆类','蔬菜类','动物类','纯热能类'
        HashMap<String, Integer> map = new HashMap<String, Integer>() {
            {
                put("谷类", 0);
                put("薯类", 0);
                put("豆类", 0);
                put("蔬菜类", 0);
                put("动物类", 0);
                put("纯热能类", 0);
            }
        };
        // 便利foodList
        for (ChildDietaryRecords childDietaryRecords : childDietaryRecordsList) {
            FoodDto[] foodsList = JSON.parseObject(childDietaryRecords.getFood(), FoodDto[].class);

            // 遍历foodList
            for (FoodDto food : foodsList) {
                Foodinfo foodinfo = foodinfoMapper.selectById(food.getId());
                food.category = foodinfo.getCategory();
                map.put(food.category, map.get(food.category) + food.number * food.energy);
            }
        }

        List<String> categoryArr = new ArrayList<>();
        suggest.append("饮食分析:\n");
        if (childinformationtable.getAge() <= 3) {
//            if (!(map.get("谷类") >= 75 && map.get("谷类") <= 125)) {
            if (map.get("谷类") < 75) {
                categoryArr.add("谷类");
                suggest.append("您缺少谷类食物\n");

            }

//            if (!(map.get("薯类") >= 75 && map.get("薯类") <= 125)) {
            if (map.get("薯类") < 75) {
                categoryArr.add("薯类");
                suggest.append("您缺少薯类食物\n");

            }

            if (map.get("豆类") < 5) {
                categoryArr.add("豆类");
                suggest.append("您缺少豆类食物\n");

            }

//            if (!(map.get("蔬菜类") >= 200 && map.get("蔬菜类") <= 400)) {
            if (map.get("蔬菜类") < 200) {
                categoryArr.add("蔬菜类");
                suggest.append("您缺少蔬菜类食物\n");

            }

//            if (!(map.get("动物类") >= 500 && map.get("动物类") <= 700)) {
            if (map.get("动物类") < 500) {
                categoryArr.add("动物类");
                suggest.append("您缺少动物类食物\n");

            }

//            if (!(map.get("纯热能类") >= 10 && map.get("纯热能类") <= 20)) {
            if (map.get("纯热能类") < 10) {
                categoryArr.add("纯热能类");
                suggest.append("您缺少纯热能类食物\n");

            }


//            QueryWrapper qw = new QueryWrapper<>();
//
//            qw.in("category", categoryArr);
//
//            return foodinfoMapper.selectList(qw);
        }
        else if (4 <= childinformationtable.getAge() && childinformationtable.getAge() <= 5) {
//            if (!(map.get("谷类") >= 100 && map.get("谷类") <= 150)) {
            if (map.get("谷类") < 100) {
                categoryArr.add("谷类");
                suggest.append("您缺少谷类食物\n");
            }

//            if (!(map.get("薯类") >= 100 && map.get("薯类") <= 150)) {
            if (map.get("薯类") < 100) {
                categoryArr.add("薯类");
                suggest.append("您缺少薯类食物\n");
            }

//            if (!(map.get("豆类") >= 15 && map.get("豆类") <= 20)) {
            if (map.get("豆类") < 15) {
                categoryArr.add("豆类");
                suggest.append("您缺少豆类食物\n");
            }

//            if (!(map.get("蔬菜类") >= 300 && map.get("蔬菜类") <= 550)) {
            if (map.get("蔬菜类") < 300) {
                categoryArr.add("蔬菜类");
                suggest.append("您缺少蔬菜类食物\n");
            }

//            if (!(map.get("动物类") >= 500 && map.get("动物类") <= 700)) {
            if (map.get("动物类") < 500) {
                categoryArr.add("动物类");
                suggest.append("您缺少动物类食物\n");

            }

//            if (!(map.get("纯热能类") >= 20 && map.get("纯热能类") <= 25)) {
            if (map.get("纯热能类") < 20) {
                categoryArr.add("纯热能类");
                suggest.append("您缺少纯热能类食物\n");
            }


//            QueryWrapper qw = new QueryWrapper<>();
//
//            qw.in("category", categoryArr);
//
//            return foodinfoMapper.selectList(qw);
        }
        else if (6 <= childinformationtable.getAge() && childinformationtable.getAge() <= 10) {
//            if (!(map.get("谷类") >= 175 && map.get("谷类") <= 250)) {
            if (map.get("谷类") < 175) {
                categoryArr.add("谷类");
                suggest.append("您缺少谷类食物\n");

            }

            if (map.get("薯类") < 175) {
                categoryArr.add("薯类");
                suggest.append("您缺少薯类食物\n");

            }

//            if (!(map.get("豆类") >= 15 && map.get("豆类") <= 15)) {
            if (map.get("豆类") < 15) {
                categoryArr.add("豆类");
                suggest.append("您缺少豆类食物\n");

            }

//            if (!(map.get("蔬菜类") >= 450 && map.get("蔬菜类") <= 500)) {
            if (map.get("蔬菜类") < 450) {
                categoryArr.add("蔬菜类");
                suggest.append("您缺少蔬菜类食物\n");

            }

//            if (!(map.get("动物类") >= 405 && map.get("动物类") <= 420)) {
            if (map.get("动物类") < 405) {
                categoryArr.add("动物类");
                suggest.append("您缺少动物类食物\n");

            }

//            if (!(map.get("纯热能类") >= 20 && map.get("纯热能类") <= 25)) {
            if (map.get("纯热能类") < 20) {
                categoryArr.add("纯热能类");
                suggest.append("您缺少纯热能类食物\n");

            }


//            QueryWrapper qw = new QueryWrapper<>();
//
//            qw.in("category", categoryArr);
//
//            return foodinfoMapper.selectList(qw);
        }
        else if (11 <= childinformationtable.getAge() && childinformationtable.getAge() <= 13) {
            if (map.get("谷类") < 250) {
                categoryArr.add("谷类");
                suggest.append("您缺少谷类食物\n");

            }

            if (map.get("薯类") < 250) {
                categoryArr.add("薯类");
                suggest.append("您缺少薯类食物\n");

            }

            if (map.get("豆类") < 15) {
                categoryArr.add("豆类");
                suggest.append("您缺少豆类食物\n");

            }

            if (map.get("蔬菜类") < 600) {
                categoryArr.add("蔬菜类");
                suggest.append("您缺少蔬菜类食物\n");

            }

            if (map.get("动物类") < 440) {
                categoryArr.add("动物类");
                suggest.append("您缺少动物类食物\n");

            }

            if (map.get("纯热能类") < 25) {
                categoryArr.add("纯热能类");
                suggest.append("您缺少纯热能类食物\n");

            }


//            QueryWrapper qw = new QueryWrapper<>();
//
//            qw.in("category", categoryArr);
//
//            return foodinfoMapper.selectList(qw);
        }
        else if (14 <= childinformationtable.getAge() && childinformationtable.getAge() <= 17) {
//            if (!(map.get("谷类") >= 300 && map.get("谷类") <= 400)) {
            if (map.get("谷类") < 300) {
                categoryArr.add("谷类");
                suggest.append("您缺少谷类食物\n");

            }

//            if (!(map.get("薯类") >= 300 && map.get("薯类") <= 400)) {
            if (map.get("薯类") < 300) {
                categoryArr.add("薯类");
                suggest.append("您缺少薯类食物\n");

            }

//            if (!(map.get("豆类") >= 15 && map.get("豆类") <= 25)) {
            if (map.get("豆类") < 15) {
                categoryArr.add("豆类");
                suggest.append("您缺少豆类食物\n");

            }

//            if (!(map.get("蔬菜类") >= 750 && map.get("蔬菜类") <= 850)) {
            if (map.get("蔬菜类") < 750) {
                categoryArr.add("蔬菜类");
                suggest.append("您缺少蔬菜类食物\n");

            }

//            if (!(map.get("动物类") >= 450 && map.get("动物类") <= 500)) {
            if (map.get("动物类") < 450) {
                categoryArr.add("动物类");
                suggest.append("您缺少动物类食物\n");

            }

//            if (!(map.get("纯热能类") >= 25 && map.get("纯热能类") <= 30)) {
            if (map.get("纯热能类") < 25) {
                categoryArr.add("纯热能类");
                suggest.append("您缺少纯热能类食物\n");
            }


//            QueryWrapper qw = new QueryWrapper<>();
//
//            qw.in("category", categoryArr);
//
//            Map<String,Object> mm = new HashMap<>();
//
//            if (suggest.toString().equals("")){
//                suggest = new StringBuilder("你的营养十分均衡，所以对您没有任何建议");
//            }
//            mm.put("suggest", suggest.toString());
//            mm.put("foodList", foodinfoMapper.selectList(qw));

//            return mm;
        }
        else{
            suggest = new StringBuilder("你的营养十分均衡，所以对您没有任何建议");
            if (map.get("谷类") < 500) {
                categoryArr.add("谷类");
                suggest.append("您缺少谷类食物\n");

            }

            if (map.get("薯类") < 500) {
                categoryArr.add("薯类");
                suggest.append("您缺少薯类食物\n");

            }

            if (map.get("豆类") < 25) {
                categoryArr.add("豆类");
                suggest.append("您缺少豆类食物\n");

            }

            if (map.get("蔬菜类") < 850) {
                categoryArr.add("蔬菜类");
                suggest.append("您缺少蔬菜类食物\n");

            }

            if (map.get("动物类") < 650) {
                categoryArr.add("动物类");
                suggest.append("您缺少动物类食物\n");

            }

            if (map.get("纯热能类") < 45) {
                categoryArr.add("纯热能类");
                suggest.append("您缺少纯热能类食物\n");
            }
        }

        suggest.append("\n饮食推荐:\n");
        for (String cate : categoryArr) {
            FoodRemVo foodRemVo = foodRemService.findRandomByCategory(cate);
            if (foodRemVo != null) {
                suggest.append(foodRemVo.getBookContent()).append("\n");
            }
        }

        Map<String,Object> mm = new HashMap<>();
        mm.put("suggest", suggest.toString());
        mm.put("foodList",new ArrayList<>());

        return mm;
    }

}

